package org.kjtc.controller;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kjtc.entity.ChargingCost;
import org.kjtc.entity.ChargingRecord;
import org.kjtc.entity.DeviceObject;
import org.kjtc.entity.Page;
import org.kjtc.mongoEntity.Log;
import org.kjtc.mongoService.LogService;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.ChargingCostService;
import org.kjtc.service.ChargingRecordService;
import org.kjtc.service.ChargingStationStatisticsService;
import org.kjtc.util.Const;
import org.kjtc.util.ExcelUtil;
import org.kjtc.util.FileDownload;
import org.kjtc.util.FileUpload;
import org.kjtc.util.Tools;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

	@Controller
	public class ChargingRecordController extends BaseController {

	@Resource(name = "chargingRecordService")
	private ChargingRecordService chargingRecordService;

	@Resource(name = "chargingCostService")
	private ChargingCostService chargingCostService;

	@Resource(name = "chargingStationStatisticsService")
	private ChargingStationStatisticsService chargingStationStatisticsService;

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@Resource(name = "logService")
	private LogService logService;

	@Resource(name = "deviceObject")
    private DeviceObject deviceObject;

	@RequestMapping (value = "/searchChargingRecord")
	@ResponseBody
	public Map<String, Object> searchChargingRecord(ChargingRecord chargingRecord, String page, String rows, String searchType)  {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录查询开始");

		// 分页查询
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		List<ChargingRecord> pageChargingRecordList = null;
		List<ChargingRecord> thisChargingRecordList = null;
		List<Object> chargingRecordTotalList = null;
		String carNumberFilePath = null;
		String equipmentNoFilePath = null;
		String datePath = null;
		File carNumberFolder = null;
		File equipmentNoFolder = null;
		String chargingStationID = null;
		String carNumber = null;
		String equipmentNo = null;
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		if(searchType != null && !searchType.isEmpty() && searchType.equals("detail")){
			chargingRecord = (ChargingRecord) this.getSession().getAttribute("searchChargingRecord");
			map.put("chargingRecord", chargingRecord);
			this.getSession().setAttribute("searchChargingRecord", null);
		}
		try {
			chargingRecord.setBranchCompanyID(this.getBranchCompanyID());
			pageQuery.setHasTotalResult(true);
			thisChargingRecordList = chargingRecordService.getThisChargingRecord(chargingRecord);
			pageQuery.setTotalResult(thisChargingRecordList.get(0).getChargingCountThis());
			pageChargingRecordList = chargingRecordService.getChargingRecordPage(chargingRecord, pageQuery);
			if (pageChargingRecordList != null && pageChargingRecordList.size() > 0) {
				for(ChargingRecord pageChargingRecord : pageChargingRecordList) {
					pageChargingRecord.setCarFile("");
					pageChargingRecord.setChargingProcessFile("");
					chargingStationID = pageChargingRecord.getChargingStationID();
					if (chargingStationID != null && !chargingStationID.isEmpty()) {
						carNumber = pageChargingRecord.getCarNumber();
						equipmentNo = pageChargingRecord.getEquipmentNo();
						if (carNumber != null && !carNumber.isEmpty()) {
							carNumberFilePath = Const.CHARGINGPROCESS_PATH + chargingStationID + File.separator + carNumber + File.separator;
							if (new File(carNumberFilePath).exists()) {
								Date startChargingDTTM = pageChargingRecord.getStartChargingDTTM();
								if (startChargingDTTM != null) {
									String startChargingDate = df.format(startChargingDTTM);
									Calendar calendar = Calendar.getInstance();
									calendar.setTime(startChargingDTTM);
									datePath = startChargingDate.substring(0, 4) + File.separator +
											startChargingDate.substring(4, 6) + File.separator + startChargingDate.substring(6, 8) + File.separator;
									// 车辆自编号文件夹
									carNumberFolder = new File(carNumberFilePath + datePath);
									if (carNumberFolder.exists()) {
										String chargingProcessFileName = null;
										String constChargingProcessFileName = carNumber + startChargingDate + Const.ZIP_SUFFIX;
										File[] chargingProcessFiles = carNumberFolder.listFiles();
										for (File chargingProcessFile : chargingProcessFiles) {
											chargingProcessFileName = chargingProcessFile.getName();
											if (chargingProcessFileName.equals(pageChargingRecord.getCarFile()) || chargingProcessFileName.equals(constChargingProcessFileName)) {
												pageChargingRecord.setCarFile(chargingProcessFileName);
//												pageChargingRecord.setChargingProcessFileCreateDate(chargingProcessFile.lastModified());
//												pageChargingRecord.setChargingProcessFileSize(chargingProcessFile.length());
											}
										}
									}
								}
							}
						}
						if (equipmentNo != null && !equipmentNo.isEmpty()) {
							equipmentNoFilePath = Const.CHARGINGPROCESS_PATH + chargingStationID + File.separator + equipmentNo + File.separator;
							if (new File(equipmentNoFilePath).exists()) {
								Date startChargingDTTM = pageChargingRecord.getStartChargingDTTM();
								if (startChargingDTTM != null) {
									String startChargingDate = df.format(startChargingDTTM);
									Calendar calendar = Calendar.getInstance();
									calendar.setTime(startChargingDTTM);
									datePath = startChargingDate.substring(0, 4) + File.separator +
											startChargingDate.substring(4, 6) + File.separator + startChargingDate.substring(6, 8) + File.separator;
									// 车辆自编号文件夹
									equipmentNoFolder = new File(equipmentNoFilePath + datePath);
									if (equipmentNoFolder.exists()) {
										String chargingProcessFileName = null;
										String constChargingProcessFileName = equipmentNo + startChargingDate + Const.ZIP_SUFFIX;
										File[] chargingProcessFiles = equipmentNoFolder.listFiles();
										for (File chargingProcessFile : chargingProcessFiles) {
											chargingProcessFileName = chargingProcessFile.getName();
											if (chargingProcessFileName.equals(constChargingProcessFileName)) {
												pageChargingRecord.setChargingProcessFile(chargingProcessFileName);
											}
										}
									}
								}
							}
						}
					}
				}
			}
			chargingRecordTotalList = new ArrayList<Object>();
			Map<String, Object> mapThis = new HashMap<String, Object>();
			mapThis.put("statisticsTypeName", "本次统计");
			mapThis.put("chargingStationCount", thisChargingRecordList.get(0).getChargingStationCountThis());
			mapThis.put("equipmentCount", thisChargingRecordList.get(0).getEquipmentCountThis());
			mapThis.put("chargingCountTotal", thisChargingRecordList.get(0).getChargingCountThis());
			mapThis.put("chargingKWHTotal", thisChargingRecordList.get(0).getChargingKWHThis());
			mapThis.put("chargingCarTotal", thisChargingRecordList.get(0).getChargingCarThis());
			chargingRecordTotalList.add(mapThis);
			mapThis = null;
			this.getSession().setAttribute("searchChargingRecord", chargingRecord);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录查询异常" + e.getMessage());
			return map;
		}
		map.put("chargingRecordList", pageChargingRecordList);
		map.put("sumChargingRecordList", chargingRecordTotalList);
		map.put("totalCnt", pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		chargingRecord = null;
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录查询结束");
		return map;
	}

	@RequestMapping (value = "/searchChargingRecordDetail")
	@ResponseBody
	public Map<String, Object> searchChargingRecordDetail(ChargingRecord chargingRecord)  {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录详细查询开始");

		List<ChargingRecord> chargingRecordList = null;
		if (chargingRecord.getID() == null || chargingRecord.getID().isEmpty()) {
			map.put("chargingRecordList", chargingRecordList);
			return map;
		}
		String carNumberFilePath = null;
		String datePath = null;
		File carNumberFolder = null;
		String chargingStationID = null;
		String carNumber = null;
		String equipmentFilePath = null;
		File equipmentFolder = null;
		String equipment = null;
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			if (chargingRecord.getBranchCompanyID() == null || chargingRecord.getBranchCompanyID().isEmpty()) {
				chargingRecord.setBranchCompanyID(this.getBranchCompanyID());
			}
			chargingRecordList = chargingRecordService.getChargingRecord(chargingRecord);
			if (chargingRecordList != null && chargingRecordList.size() > 0) {
				for(ChargingRecord pageChargingRecord : chargingRecordList) {
					pageChargingRecord.setChargingProcessFile("");
					pageChargingRecord.setCarFile("");
					chargingStationID = pageChargingRecord.getChargingStationID();
					if (chargingStationID != null && !chargingStationID.isEmpty()) {
						carNumber = pageChargingRecord.getCarNumber();
						equipment = pageChargingRecord.getEquipmentNo();
						if (carNumber != null && !carNumber.isEmpty()) {
							carNumberFilePath = Const.CHARGINGPROCESS_PATH + chargingStationID + File.separator + carNumber + File.separator;
							if (new File(carNumberFilePath).exists()) {
								Date startChargingDTTM = pageChargingRecord.getStartChargingDTTM();
								if (startChargingDTTM != null) {
									String startChargingDate = df.format(startChargingDTTM);
									Calendar calendar = Calendar.getInstance();
									calendar.setTime(startChargingDTTM);
									datePath = startChargingDate.substring(0, 4) + File.separator +
											startChargingDate.substring(4, 6) + File.separator + startChargingDate.substring(6, 8) + File.separator;
									// 车辆自编号文件夹
									carNumberFolder = new File(carNumberFilePath + datePath);
									if (carNumberFolder.exists()) {
										String chargingProcessFileName = null;
										String constChargingProcessFileName = carNumber + startChargingDate + Const.ZIP_SUFFIX;
										File[] chargingProcessFiles = carNumberFolder.listFiles();
										for (File chargingProcessFile : chargingProcessFiles) {
											chargingProcessFileName = chargingProcessFile.getName();
											if (chargingProcessFileName.equals(pageChargingRecord.getCarFile()) || chargingProcessFileName.equals(constChargingProcessFileName)) {
												pageChargingRecord.setCarFile(chargingProcessFileName);
												pageChargingRecord.setCarFileCreateDate(chargingProcessFile.lastModified());
												pageChargingRecord.setCarFileSize(chargingProcessFile.length());
											}
										}
									}
								}
							}
						}
						if (equipment != null && !equipment.isEmpty()) {
							equipmentFilePath = Const.CHARGINGPROCESS_PATH + chargingStationID + File.separator + equipment + File.separator;
							if (new File(equipmentFilePath).exists()) {
								Date startChargingDTTM = pageChargingRecord.getStartChargingDTTM();
								if (startChargingDTTM != null) {
									String startChargingDate = df.format(startChargingDTTM);
									Calendar calendar = Calendar.getInstance();
									calendar.setTime(startChargingDTTM);
									datePath = startChargingDate.substring(0, 4) + File.separator +
											startChargingDate.substring(4, 6) + File.separator + startChargingDate.substring(6, 8) + File.separator;
									// 车辆自编号文件夹
									equipmentFolder = new File(equipmentFilePath + datePath);
									if (equipmentFolder.exists()) {
										String chargingProcessFileName = null;
										String constChargingProcessFileName = equipment + startChargingDate + Const.ZIP_SUFFIX;
										File[] chargingProcessFiles = equipmentFolder.listFiles();
										for (File chargingProcessFile : chargingProcessFiles) {
											chargingProcessFileName = chargingProcessFile.getName();
											if (chargingProcessFileName.equals(pageChargingRecord.getChargingProcessFile()) || chargingProcessFileName.equals(constChargingProcessFileName)) {
												pageChargingRecord.setChargingProcessFile(chargingProcessFileName);
												pageChargingRecord.setChargingProcessFileCreateDate(chargingProcessFile.lastModified());
												pageChargingRecord.setChargingProcessFileSize(chargingProcessFile.length());
											}
										}
									}
								}
							}
						}
					}
				}
			}
			this.getSession().setAttribute("searchChargingRecordDetail", chargingRecord);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录详细查询异常" + e.getMessage());
			return map;
		}
		map.put("chargingRecordList", chargingRecordList);
		chargingRecord = null;
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录详细查询结束");
		return map;
	}

	@RequestMapping (value = "/searchSumChargingRecord")
	@ResponseBody
	public Map<String, Object> searchSumChargingRecord(ChargingRecord chargingRecord)  {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录统计查询开始");

		List<ChargingRecord> sumChargingRecordList = null;
		List<Object> chargingRecordTotalList = null;
		try {
			sumChargingRecordList = chargingRecordService.getSumChargingRecord(chargingRecord);
			chargingRecordTotalList = new ArrayList<Object>();
			Map<String, Object> mapDay = new HashMap<String, Object>();
			Map<String, Object> mapMonth = new HashMap<String, Object>();
			Map<String, Object> mapTotal = new HashMap<String, Object>();
			for (ChargingRecord sumChargingRecordInfo : sumChargingRecordList) {
				sumChargingRecordInfo.setChargingTimeTotal(Tools.formatTime(sumChargingRecordInfo.getChargingDTTMTotal()));
				mapDay.put("statisticsTypeName", "当日统计");
				mapDay.put("chargingStationCount", sumChargingRecordInfo.getChargingStationCountDay());
				mapDay.put("equipmentCount", sumChargingRecordInfo.getEquipmentCountDay());
				mapDay.put("chargingCountTotal", sumChargingRecordInfo.getChargingCountDay());
				mapDay.put("chargingKWHTotal", sumChargingRecordInfo.getChargingKWHDay());
				mapDay.put("chargingCarTotal", sumChargingRecordInfo.getChargingCarDay());
				chargingRecordTotalList.add(mapDay);
				mapDay = null;

				mapMonth.put("statisticsTypeName", "当月统计");
				mapMonth.put("chargingStationCount", sumChargingRecordInfo.getChargingStationCountMonth());
				mapMonth.put("equipmentCount", sumChargingRecordInfo.getEquipmentCountMonth());
				mapMonth.put("chargingCountTotal", sumChargingRecordInfo.getChargingCountMonth());
				mapMonth.put("chargingKWHTotal", sumChargingRecordInfo.getChargingKWHMonth());
				mapMonth.put("chargingCarTotal", sumChargingRecordInfo.getChargingCarMonth());
				chargingRecordTotalList.add(mapMonth);
				mapMonth = null;

				mapTotal.put("statisticsTypeName", "累计统计");
				mapTotal.put("chargingStationCount", sumChargingRecordInfo.getChargingStationCountTotal());
				mapTotal.put("equipmentCount", sumChargingRecordInfo.getEquipmentCountTotal());
				mapTotal.put("chargingCountTotal", sumChargingRecordInfo.getChargingCountTotal());
				mapTotal.put("chargingKWHTotal", sumChargingRecordInfo.getChargingKWHTotal());
				mapTotal.put("chargingCarTotal", sumChargingRecordInfo.getChargingCarTotal());
				chargingRecordTotalList.add(mapTotal);
				mapTotal = null;
			}
			sumChargingRecordList = null;
			this.getSession().setAttribute("searchChargingRecord", chargingRecord);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录统计查询异常" + e.getMessage());
			return map;
		}
		map.put("sumChargingRecordList", chargingRecordTotalList);
		chargingRecord = null;
		chargingRecordTotalList = null;
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录统计查询结束");
		return map;
	}

	@RequestMapping(value = "exportChargingRecord")
	@ResponseBody
	public Map<String, Object> exportChargingRecord(@RequestParam(value="checkFlag", required=false) String checkFlag, HttpServletResponse response) {

		Map<String, Object> map = new HashMap<String, Object>();
		JSONArray jsonArray = null;
		try {
			jsonArray = Tools.readJsonFile(Const.CHARGINFRECORD_EXPORTJSONPATH, false);
			if (jsonArray == null) {
				map.put("result", "JsonError");
			} else {
				if (checkFlag != null && !checkFlag.isEmpty()) {
					logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导出开始");
					try{
						ChargingRecord chargingRecord = (ChargingRecord) this.getSession().getAttribute("searchChargingRecord");
						if (chargingRecord != null) {
							chargingRecord.setLimitCount(1000);
							List<ChargingRecord> chargingRecordList = chargingRecordService.getChargingRecord(chargingRecord);
							List<ChargingRecord> sumChargingRecordList = chargingRecordService.getSumExportChargingRecord(chargingRecord);
							for (ChargingRecord chargingRecordInfo : chargingRecordList) {
								chargingRecordInfo.setChargingDate(Tools.date2Str(chargingRecordInfo.getStartChargingDTTM(), "yyyy-MM-dd"));
								chargingRecordInfo.setTotalChargingTime(Tools.formatTime(chargingRecordInfo.getTotalChargingDTTM()));
							}
							for (ChargingRecord sumChargingRecordInfo : sumChargingRecordList) {
								sumChargingRecordInfo.setChargingTimeTotal(Tools.formatTime(sumChargingRecordInfo.getChargingDTTMTotal()));
							}
							map.put("chargingRecordList", chargingRecordList);
							map.put("sumExportChargingRecordList", sumChargingRecordList);
							ExcelUtil.exportExcel(Const.CHARGINFRECORD_EXCELNAME, jsonArray, map, response);
						}
					} catch (Exception e){
						logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导出异常：" + e.getMessage());
					}
					logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导出结束");
					actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "充电记录导出");
				} else {
					map.put("result", "Success");
				}
			}
		} catch (Exception e) {
			map.put("result", "JsonError");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导出的JSON模板读取异常：" + e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "exportChargingProcess")
	@ResponseBody
	public Map<String, Object> exportChargingProcess(@RequestParam(value="subfolder", required=false) String subfolder, @RequestParam(value="fileName", required=false) String fileName,
			@RequestParam(value="checkFlag", required=false) String checkFlag, HttpServletResponse response) {

		Map<String, Object> map = new HashMap<String, Object>();
		String filePath = Const.CHARGINGPROCESS_PATH + subfolder + File.separator;
		if (fileName != null && !fileName.isEmpty() && fileName.length() > 18) {
			int fileLength = fileName.length();
			filePath = filePath + fileName.substring(0, fileLength - 18) + File.separator +
					fileName.substring(fileLength - 18, fileLength - 14) + File.separator +
					fileName.substring(fileLength - 14, fileLength - 12) + File.separator +
					fileName.substring(fileLength - 12, fileLength - 10) + File.separator + fileName;
			if (fileName.indexOf(Const.ZIP_SUFFIX) < 0) {
				filePath = filePath + Const.ZIP_SUFFIX;
				fileName = fileName + Const.ZIP_SUFFIX;
			}
			if (checkFlag != null && !checkFlag.isEmpty()) {
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程文件下载开始");
				try{
					FileDownload.fileDownload(response, filePath, fileName);
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程文件下载:" + Const.CHARGINGPROCESS_PATH + subfolder + File.separator + fileName);
				} catch (Exception e){
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程文件下载异常" + e.getMessage());
				}
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程文件下载结束");
				actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "充电过程文件下载");
			} else {
				File file = new File(filePath);
				if (file.exists()) {
					map.put("result", "Success");
				} else {
					map.put("result", "NotFound");
				}
			}
		} else {
			map.put("result", "NotFound");
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "importChargingRecord")
	@ResponseBody
	public Map<String, Object> importChargingRecord(@RequestParam(value="importFile", required=false) MultipartFile file) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导入开始");
		if (file != null && !file.isEmpty()) {
			try {
				List<ChargingRecord> importChargingRecordList = (List<ChargingRecord>) ExcelUtil.importExcel(file.getInputStream(), Const.CHARGINFRECORD_IMPORTJSONPATH, ChargingRecord.class);
				if (importChargingRecordList == null) {
					map.put("result", "JsonError");
					logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "导入失败，导入的JSON模板错误 或 不存在！");
				} else {
					int length = importChargingRecordList.size();
					if (length > 0) {
						String totalChargingTime = null;
						int totalChargingDTTM = 0;
						for (ChargingRecord chargingRecordInfo : importChargingRecordList) {
							if (chargingRecordInfo.getTotalChargingTime() != null && !chargingRecordInfo.getTotalChargingTime().isEmpty()) {
								totalChargingTime = chargingRecordInfo.getTotalChargingTime();
								totalChargingDTTM = 0;
								if (totalChargingTime.indexOf("小时") >= 0) {
									totalChargingDTTM = totalChargingDTTM + Integer.parseInt(totalChargingTime.substring(0, totalChargingTime.indexOf("小时"))) * 60 * 60;
									totalChargingTime = totalChargingTime.substring(totalChargingTime.indexOf("小时")).replace("小时", "");
								}
								if (totalChargingTime.indexOf("分") >= 0) {
									totalChargingDTTM = totalChargingDTTM + Integer.parseInt(totalChargingTime.substring(0, totalChargingTime.indexOf("分"))) * 60;
									totalChargingTime = totalChargingTime.substring(totalChargingTime.indexOf("分")).replace("分", "");
								}
								if (totalChargingTime.indexOf("秒") >= 0) {
									totalChargingDTTM = totalChargingDTTM + Integer.parseInt(totalChargingTime.substring(0, totalChargingTime.indexOf("秒")));
									totalChargingTime = totalChargingTime.substring(totalChargingTime.indexOf("秒")).replace("秒", "");
								} else {
									totalChargingDTTM = Integer.parseInt(totalChargingTime);
								}
							}
							chargingRecordInfo.setTotalChargingDTTM(totalChargingDTTM);
						}
						totalChargingTime = null;
						int count = chargingRecordService.saveChargingRecord(importChargingRecordList, this.getUesrInfo().getUserID(), "1");
						map.put("result", "Success");
						map.put("count", length - count);
						String message = "充电记录文件：【" + file.getOriginalFilename() + "】导入成功";
						if (length - count > 0) {
							message = message + "，其中有" + (length - count) + "条重复数据被忽略！";
						}
						logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + message);
					} else {
						map.put("result", "NotFound");
						logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "导入失败，文件内没有可导入数据!");
					}
				}

			} catch (Exception e) {
				map.put("result", "Error");
				logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录文件【" +  file.getOriginalFilename() + "】导入异常：" + e.getMessage());
				e.printStackTrace();
			}
			actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "充电记录导入：" + file.getOriginalFilename());
		} else {
			map.put("result", "NotFound");
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "导入失败，导入文件不存在!");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录导入结束");
		return map;
	}

	@RequestMapping(value = "importChargingProcess")
	@ResponseBody
	public Map<String, Object> importChargingProcess(@RequestParam(value="importFile", required=false) MultipartFile[] files,
			@RequestParam(value="subfolder", required=false) String subfolder) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程上传开始");
		if (subfolder != null && !subfolder.isEmpty()) {
			String filePath = null;
			int length = files.length;
			List<String> chargingProcessFileList = new ArrayList<String>();
			List<String> resultList = new ArrayList<String>();
			File file = null;
			int fileLength = 0;
			for (int i = 0; i < length; i++ ) {
				filePath = Const.CHARGINGPROCESS_PATH;
				String chargingProcessFileName = files[i].getOriginalFilename();
				if (files[i] != null && !files[i].isEmpty()) {
					filePath = filePath + subfolder + File.separator;
					if (chargingProcessFileName != null && !chargingProcessFileName.isEmpty() && chargingProcessFileName.length() > 18) {
						fileLength = chargingProcessFileName.length();
						if (!Tools.isNumeric(chargingProcessFileName.substring(fileLength - 18, fileLength - 4))) {
							map.put("result", "NameError");
							resultList.add("NameError");
							chargingProcessFileList.add(chargingProcessFileName);
							logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程文件【" +  chargingProcessFileName + "】名称错误，必须是'车自编号或桩号+开始充电时间'！");
						} else {
							filePath = filePath + chargingProcessFileName.substring(0, fileLength - 18) + File.separator +
									chargingProcessFileName.substring(fileLength - 18, fileLength - 14) + File.separator +
									chargingProcessFileName.substring(fileLength - 14, fileLength - 12) + File.separator +
									chargingProcessFileName.substring(fileLength - 12, fileLength - 10) + File.separator;
							file = new File(filePath + chargingProcessFileName);
							if (file.exists()) {
								map.put("result", "Exists");
								resultList.add("Exists");
								chargingProcessFileList.add(chargingProcessFileName);
								logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "上传充电过程文件已存在！");
							} else {
								try {
									FileUpload.upload(files[i], filePath, chargingProcessFileName.replace(",", ""), null);
									map.put("result", "Success");
									map.put("fileSize", files[i].getSize());
									resultList.add("Success");
									chargingProcessFileList.add(chargingProcessFileName);
									logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "上传充电过程文件【" + chargingProcessFileName + "】已成功上传到" + filePath + chargingProcessFileName);
									actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "充电过程上传：" + files[i].getOriginalFilename());
								} catch (Exception e) {
									map.put("result", "Error");
									resultList.add("Error");
									chargingProcessFileList.add(chargingProcessFileName);
									logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程文件【" +  chargingProcessFileName + "】上传异常：" + e.getMessage());
									e.printStackTrace();
								}
							}
						}
					} else {
						map.put("result", "NameError");
						resultList.add("NameError");
						chargingProcessFileList.add(chargingProcessFileName);
						logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程文件【" +  chargingProcessFileName + "】名称错误，长度不能小于18！");
					}
				} else {
					map.put("result", "NotFound");
					resultList.add("NotFound");
					chargingProcessFileList.add(chargingProcessFileName);
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程文件【" +  chargingProcessFileName + "】不存在！");
				}
			}
			map.put("resultList", resultList);
			map.put("chargingProcessFileList", chargingProcessFileList);
			resultList = null;
			chargingProcessFileList = null;
		} else {
			map.put("result", "ChargingStationIDNotFound");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电站ID为空！");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程上传结束");
		return map;
	}

	@RequestMapping(value = "searchChargingCost")
	@ResponseBody
	public Map<String, Object> searchChargingCost(ChargingCost chargingCost, String page, String rows) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "查询费用开始");
		// 分页查询
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		List<ChargingCost>chargingCosts = null;
		 try {
			chargingCosts = chargingCostService.getChargingCostPage(chargingCost, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "查询费用异常" + e.getMessage());
			return map;
		}
		map.put("totalCnt", pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("chargingCostsList", chargingCosts);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "查询费用结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "查询费用");
		chargingCost = null;
		return map;
	}
	/**
	 * 费用管理添加
	 */
	@ResponseBody
	@RequestMapping(value = "/saveCharingCost")

	public Map<String, Object> saveCharingCost(ChargingCost chargingCost){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理添加开始");
		chargingCost.setEffectiveDate(Tools.str2Date(chargingCost.getChargingCostDate()));
		chargingCost.setCreateUser(this.getUesr().getUserID());
		chargingCost.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = chargingCostService.insertChargingCost(chargingCost);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("PRIMARY".equals(error[3].trim())){
				map.put("result", "EffectiveDateNoNull");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理添加异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理添加结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "费用管理添加");
		chargingCost = null;
		return map;
	}
	/**
	 * 费用管理修改
	 * @throws Exception
	 */
   @ResponseBody
   @RequestMapping(value = "/updateCharingCost")
   public Map<String, Object> updateCharingCost(ChargingCost chargingCost){

	   Map<String, Object> map = new HashMap<String, Object>();
	   logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理修改开始");
	   chargingCost.setEffectiveDate(Tools.str2Date(chargingCost.getChargingCostDate()));
	   chargingCost.setUpdateUser(this.getUesr().getUserID());
       int count = 0;
       try{
		   count = chargingCostService.updateChargingCost(chargingCost);
		   if(count > 0) {
			   	map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
       } catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("ChargingStationID".equals(error[3].trim())) {
				map.put("result", "ChargingStationIDNoNull");
			} else if ("EffectiveDate".equals(error[3].trim())){
				map.put("result", "EffectiveDateNoNull");
			}
       } catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理修改异常:" + e.getMessage());
		}
	   logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理修改结束");
	   actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "费用管理修改");
	   chargingCost = null;
	   return map;
    }
   /**
    * 费用管理删除
    * @throws Exception
    */
   @ResponseBody
   @RequestMapping(value = "/deleteCharingCost")
   public Object deleteChargingCost(String effectiveDateList) {

	   Map<String, Object> map = new HashMap<String, Object>();
	   logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理删除开始");
	   int count = 0 ;
	   ChargingCost chargingCost = new ChargingCost();
	   try{
		   chargingCost.setEffectiveDateList(effectiveDateList.split(","));
    	   count = chargingCostService.deleteChargingCost(chargingCost);
	      if (count > 0) {
	        map.put("result","Success");
	      }else{
	        map.put("result","NotFound");
	      }
	   }catch (Exception e) {
	        e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理删除异常:" + e.getMessage());
	   }
	   logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "费用管理删除结束");
	   actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "费用管理删除");
	   chargingCost = null;
	   return map;
   }

   	@RequestMapping(value = "/searchChargingProcess")
   	@ResponseBody
   	public Map<String, Object> searchChargingProcess(ChargingRecord chargingRecord, String page, String rows){

   		Map<String, Object> map = new HashMap<String, Object>();
   		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电进程信息查询开始");
       	try {
       		chargingRecord.setBranchCompanyID(this.getBranchCompanyID());
       		File chargingProcessPath = new File(Const.CHARGINGPROCESS_PATH);
       		if (chargingProcessPath.exists()) {
	       		List<Object> chargingProcessList = null;
	       		List<Object> chargingProcessFileList = null;
	       		String chargingProcessFileName = null;
	       		Map<String, Object> stationMap = null;
	       		int totalCnt = 0;
	       		int totalPage = 0;
	       		int pageNo = Integer.parseInt(page);
	       		int row = Integer.parseInt(rows);
	       		int startCnt = (pageNo - 1) * row;
	       		int endCnt = pageNo * row;
	       		File [] chargingProcessFiles = chargingProcessPath.listFiles();
	       		map.put("totalCnt", totalCnt);
	       		String chargingStationID = chargingRecord.getChargingStationID();
	       		String chargingProcessType = chargingRecord.getChargingProcessType();
	       		File chargingProcessSubPath = null;
	       		File [] chargingProcessSubFiles = null;
//		       		Date chargingProcessDate = null;
	       		Date startChargingDate = Tools.str2Date(chargingRecord.getStartChargingDate());
	       		Date endChargingDate = Tools.str2Date(chargingRecord.getEndChargingDate());
	 	  		for(File chargingProcessFile : chargingProcessFiles){
	 	  			chargingProcessFileList = new ArrayList<Object>();
	 	  			chargingProcessFileName = chargingProcessFile.getName();
	 	  			if (chargingStationID == null || chargingStationID.equals("") || chargingProcessFileName.equals(chargingStationID)) {
	 	  				if (chargingProcessFile.isDirectory()) {
	 	  					if (chargingProcessType != null && !chargingProcessType.isEmpty()) {
	 	  						chargingProcessSubPath = new File(chargingProcessFile.getPath());
		 	  					chargingProcessSubFiles = chargingProcessSubPath.listFiles();
		 	  					for(File chargingProcessSubFile : chargingProcessSubFiles){
		 	  						if (("carNumber".equals(chargingProcessType) && chargingProcessSubFile.getName() != null && chargingProcessSubFile.getName().length() == 8) ||
		 	  							("equipmentNO".equals(chargingProcessType) && chargingProcessSubFile.getName() != null && chargingProcessSubFile.getName().length() == 5) ||
		 	  							("equipmentID".equals(chargingProcessType) && chargingProcessSubFile.getName() != null && chargingProcessSubFile.getName().length() == 10)) {
		 	  							searchFileNameList(map, chargingProcessSubFile.getPath(), chargingProcessFileList, startCnt, endCnt, startChargingDate, endChargingDate);
		 	  						}
		 	  					}
	 	  					} else {
	 	  						searchFileNameList(map, chargingProcessFile.getPath(), chargingProcessFileList, startCnt, endCnt, startChargingDate, endChargingDate);
	 	  					}
		       			} else {
		       				totalCnt = (int)map.get("totalCnt");
		       				map.put("totalCnt", totalCnt + 1);
		       				if (totalCnt > startCnt && totalCnt <= endCnt) {
		       					Map<String, Object> chargingProcessFileMap = new HashMap<String, Object>();
		       					if (startChargingDate == null && endChargingDate == null) {
		       						chargingProcessFileMap.put("chargingProcessFileName", chargingProcessFileName);
    	       						chargingProcessFileMap.put("chargingProcessFileSize", chargingProcessFile.length());
    	       						chargingProcessFileMap.put("creatDate", chargingProcessFile.lastModified());
       								chargingProcessFileList.add(chargingProcessFileMap);
		       					}
//			       					else {
//			       						int fileLength = chargingProcessFileName.length();
//			       						if (fileLength > 18) {
//			       							Date chargingProcessDate = Tools.str2Date(chargingProcessFileName.substring(fileLength - 18, fileLength - 14) + "-" +
//			       									chargingProcessFileName.substring(fileLength - 14, fileLength - 12) + "-" + chargingProcessFileName.substring(fileLength - 12, fileLength - 10));
//			       							if ((startChargingDate == null || chargingProcessDate.compareTo(startChargingDate) >= 0) &&
//			       									(endChargingDate == null || chargingProcessDate.compareTo(endChargingDate) <= 0)) {
//			       								chargingProcessFileMap.put("chargingProcessFileName", chargingProcessFileName);
//			    	       						chargingProcessFileMap.put("chargingProcessFileSize", chargingProcessFile.length());
//			    	       						chargingProcessFileMap.put("creatDate", chargingProcessFile.lastModified());
//			       								chargingProcessFileList.add(chargingProcessFileMap);
//			       							} else {
//				       							map.put("totalCnt", totalCnt - 1);
//				       						}
//			       						} else {
//			       							map.put("totalCnt", totalCnt - 1);
//			       						}
//			       					}
		       				}
		       			}
		 	  			if (chargingProcessFileList != null && chargingProcessFileList.size() > 0) {
		 	  				stationMap = new HashMap<String, Object>();
		 	  				stationMap.put("chargingStationID", chargingProcessFileName);
			 	  			stationMap.put("chargingProcessFileList", chargingProcessFileList);
			 	  			if (chargingProcessList == null) {
			 	  				chargingProcessList = new ArrayList<Object>();
			 	  			}
			 	  			chargingProcessList.add(stationMap);
		 	  			}
	 	  			}
	       		}
	 	  		if (chargingProcessList != null) {
	 	  			map.put("result", "Sucess");
	 	  			map.put("chargingProcessList", chargingProcessList);
	 	  			totalCnt = (int)map.get("totalCnt");
	 	  			if (totalCnt % row > 0) {
	 	  				totalPage = totalCnt / row + 1;
	 	  			} else {
	 	  				totalPage = totalCnt / row;
	 	  			}
	 				map.put("totalPage", totalPage);
	 	  		} else {
 	  				map.put("result", "NotFound");
 	  			}
	 	  		chargingProcessList = null;
	 	  		chargingProcessFiles = null;
	       		chargingProcessFileList = null;
       		} else {
       			map.put("result", "NotFound");
       		}
		} catch (Exception e) {
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程信息查询异常:" + e.getMessage());
		}
   		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "充电过程信息查询");
   		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电过程信息查询结束");
   		return map;
   	}

   	private void searchFileNameList(Map<String, Object> map, String path, List<Object> chargingProcessFileList, int startCnt, int endCnt, Date startChargingDate, Date endChargingDate){
   		File chargingProcessPath = new File(path);
   		if (chargingProcessPath.exists()) {
   			File [] chargingProcessFiles = chargingProcessPath.listFiles();
   			Arrays.sort(chargingProcessFiles, new Comparator<File>() {
				@Override
				public int compare(File f1, File f2) {
					return f2.getName().compareTo(f1.getName());
				}
   			});
   			for(File chargingProcessFile : chargingProcessFiles){
       			if (chargingProcessFile.isDirectory()) {
       				searchFileNameList(map, chargingProcessFile.getPath(), chargingProcessFileList, startCnt, endCnt, startChargingDate, endChargingDate);
       			} else {
       				map.put("totalCnt", (int)map.get("totalCnt") + 1);
       				if ((int)map.get("totalCnt") > startCnt && (int)map.get("totalCnt") <= endCnt) {
       					Map<String, Object> chargingProcessFileMap = new HashMap<String, Object>();
       					String chargingProcessFileName = chargingProcessFile.getName();
       					if (startChargingDate == null && endChargingDate == null) {
       						chargingProcessFileMap.put("chargingProcessFileName", chargingProcessFileName);
       						chargingProcessFileMap.put("chargingProcessFileSize", chargingProcessFile.length());
       						chargingProcessFileMap.put("creatDate", chargingProcessFile.lastModified());
       						chargingProcessFileList.add(chargingProcessFileMap);
       					} else {
       						int fileLength = chargingProcessFileName.length();
       						if (fileLength > 18) {
       							Date chargingProcessDate = Tools.str2Date(chargingProcessFileName.substring(fileLength - 18, fileLength - 14) + "-" +
       									chargingProcessFileName.substring(fileLength - 14, fileLength - 12) + "-" + chargingProcessFileName.substring(fileLength - 12, fileLength - 10));
       							if ((startChargingDate == null || chargingProcessDate.compareTo(startChargingDate) >= 0) &&
       									(endChargingDate == null || chargingProcessDate.compareTo(endChargingDate) <= 0)) {
       								chargingProcessFileMap.put("chargingProcessFileName", chargingProcessFileName);
    	       						chargingProcessFileMap.put("chargingProcessFileSize", chargingProcessFile.length());
    	       						chargingProcessFileMap.put("creatDate", chargingProcessFile.lastModified());
       								chargingProcessFileList.add(chargingProcessFileMap);
       							} else {
	       							map.put("totalCnt", (int)map.get("totalCnt") - 1);
	       						}
       						} else {
       							map.put("totalCnt", (int)map.get("totalCnt") - 1);
       						}
       					}
       				}
       			}
	  		}
   		}
   		chargingProcessPath = null;
   	}

	@RequestMapping(value = "chargingChartHistory")
	@ResponseBody
    public Object getChargingStatus(ChargingRecord chargingRecord){

		Map<String, Object> map = new HashMap<String, Object>();
		Log mongoLog = null;
		List<Log> mongoLogList = null;

		try {
			List<Object> outputVoltage = null;
			List<Object> outputElectric = null;
			List<Object> currentSOC = null;
			mongoLog = new Log();
			String logMessage = null;
			if (chargingRecord.getStartChargingTime() != null && !chargingRecord.getStartChargingTime().isEmpty() && !"-".equals(chargingRecord.getStartChargingTime())) {
				logMessage = "\"startChargingDTTM\":\"" + chargingRecord.getStartChargingTime() + ",";
				if (chargingRecord.getCardNo() != null && !chargingRecord.getCardNo().isEmpty()) {
					logMessage = logMessage + "\"cardNo\":\"" + chargingRecord.getCardNo() + ",";
				}
				if (chargingRecord.getCarNumber() != null && !chargingRecord.getCarNumber().isEmpty()) {
					logMessage = logMessage + "\"carNumber\":\"" + chargingRecord.getCarNumber() + ",";
				}
				logMessage = logMessage + "\"equipmentID\":\"" + chargingRecord.getEquipmentNo();
				mongoLog.setMessage(logMessage);
				mongoLog.setStartTime(chargingRecord.getStartChargingTime());
				if (chargingRecord.getEndChargingTime() == null || chargingRecord.getEndChargingTime().isEmpty()) {
					mongoLog.setEndTime(Tools.date2Str(new Date()));
				} else {
					mongoLog.setEndTime(Tools.date2Str(new Date(Tools.str2DateTime(chargingRecord.getEndChargingTime()).getTime() + 60000)));
				}
			} else {
				logMessage = "\"cardNo\":\"" + chargingRecord.getCardNo() + ",";
				logMessage = logMessage + "\"carNumber\":\"" + chargingRecord.getCarNumber() + ",";
				logMessage = logMessage + "\"equipmentID\":\"" + chargingRecord.getEquipmentNo();
				mongoLog.setMessage(logMessage);
				if (chargingRecord.getStartChargingDate() != null && !chargingRecord.getStartChargingDate().isEmpty()) {
					mongoLog.setStartTime(chargingRecord.getStartChargingDate());
				} else {
					map.put("result", "Error");
					return map;
				}
				if (chargingRecord.getEndChargingDate() != null && !chargingRecord.getEndChargingDate().isEmpty()) {
					mongoLog.setEndTime(Tools.date2Str(new Date(Tools.str2DateTime(chargingRecord.getEndChargingDate()).getTime() + 360000)));
				} else {
					map.put("result", "Error");
					return map;
				}
			}

			mongoLogList = logService.searchChargingLog(mongoLog);
			mongoLog = null;

			int logLength = mongoLogList.size();
			if (logLength > 0) {
				outputVoltage = new ArrayList<Object>();
				outputElectric = new ArrayList<Object>();
				currentSOC = new ArrayList<Object>();
				List<String> tempOutputVoltage = null;
				List<String> tempOutputElectric = null;
				List<String> tempCurrentSOC = null;
				for (Log log : mongoLogList) {
					JSONObject messageJson = null;
					if (log.getMessage().indexOf("realTimeData") > 0) {
						messageJson = JSONObject.fromObject(log.getMessage().substring(log.getMessage().indexOf(":") + 1));
					} else {
						messageJson = JSONObject.fromObject(log.getMessage());
					}
					if (messageJson.has("equipmentData")) {
						JSONArray jsonArray = messageJson.getJSONArray("equipmentData");
						int jsonLength = jsonArray.size();
						JSONObject json = null;
						for (int j = 0; j < jsonLength; j++) {
							json = jsonArray.getJSONObject(j);
							if (json != null && json.getString("equipmentID").equals(chargingRecord.getEquipmentNo())) {
								tempOutputVoltage = new ArrayList<String>();
								tempOutputVoltage.add(Tools.date2Str(log.getTimestamp()));
								if (json.has("outputVoltage")) {
									tempOutputVoltage.add(json.getString("outputVoltage"));
								} else {
									tempOutputVoltage.add("0");
								}
								outputVoltage.add(tempOutputVoltage);
								tempOutputVoltage = null;

								tempOutputElectric = new ArrayList<String>();
								tempOutputElectric.add(Tools.date2Str(log.getTimestamp()));
								if (json.has("outputElectric")) {
									tempOutputElectric.add(json.getString("outputElectric"));
								} else {
									tempOutputElectric.add("0");
								}
								outputElectric.add(tempOutputElectric);
								tempOutputElectric = null;

								tempCurrentSOC = new ArrayList<String>();
								tempCurrentSOC.add(Tools.date2Str(log.getTimestamp()));
								if (json.has("currentSOC")) {
									tempCurrentSOC.add(json.getString("currentSOC"));
								} else {
									tempCurrentSOC.add("0");
								}
								currentSOC.add(tempCurrentSOC);
								tempCurrentSOC = null;
							}
						}
					}
				}
			}
			map.put("result", "Sucess");
			map.put("outputVoltage", outputVoltage);
			map.put("outputElectric", outputElectric);
			map.put("currentSOC", currentSOC);
			outputVoltage = null;
			outputElectric = null;
			currentSOC = null;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电记录历史充电曲线信息获取异常:" + e.getMessage());
		}
		return map;
	}
}
